
var onPageCount = 0;

$(function() {

    //f1();
    /*
    var btnTest = $("<input type=\"button\" value=\"test\">");
    btnTest.css("position", "fixed");
    btnTest.css("top", "15%");
    btnTest.css("left", "15%");
    $("#contentContainer").append(btnTest);
    btnTest.click(function() {
        var contentContainer = document.getElementById("contentContainer");
        appendContentLine(contentContainer.clientWidth);

    });*/
    var container = document.getElementById("contentContainer");

    var scrollTop    = document.documentElement.scrollTop    || document.body.scrollTop;
    var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
    var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
    var loop = true;




    while (windowHeight == scrollHeight  &&  loop) {
        windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
        scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
        loop = appendContentLine(container.clientWidth);
        ++onPageCount;
    }

    window.onscroll = autoAppend;
});

//滚动到底新增内容的方法
function autoAppend(){
    var scrollTop    = document.documentElement.scrollTop    || document.body.scrollTop;
    var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
    var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
    if (scrollTop + windowHeight > scrollHeight * 0.9) {
        appendContentPage();
    }
}

//新增一页
function appendContentPage(){
    var loop = true;
    for(var i = 0; i < onPageCount  &&  loop; ++i){
        loop = appendContentLine(contentContainer.clientWidth);
    }
}

//新增一个可分行整体
function appendContentLine(sizeX) {
    return contentRandomBinaryDividingAppend(sizeX, parseInt(0.4 * sizeX), 4);
}

function contentRandomBinaryDividingAppend(x, y, count) {  
    if(count > 1) {
        if((parseInt(Math.random() * 10) & 1) == 1) {
            if(y * 2 / x > 3) y /= 2; else x /= 2;
        }
        else {
            if(x * 2 / y > 3) x /= 2; else y /= 2;
        }
    }
    
    if((parseInt(Math.random() * 10) & 1) == 1) {
        if(!appendAContent(x, y, true)) return false;
    } else {
        if(!appendAContent(x, y, false)) return false;
    }
    if(count > 1) return contentRandomBinaryDividingAppend(x, y, --count);
    return true;
}

function appendAContent(sizeX, sizeY, left) {
    var newDiv = $("<div class=\"content\" style=\"float:" + (left? "left": "right") + "; height:" + (sizeY-12) + "px; width: " + (sizeX-12) + "px;\"></div>");
    
    $("#contentContainer").append(newDiv);
    return true;
}
/*
function f1() {
    var sizeX, sizeY;
    
    var colors = new Array();
    colors[0] = "pink";
    colors[1] = "yellow";
    colors[2] = "gray";
    colors[3] = "green";
    colors[4] = "orange";

    var sizes = new Array();

    sizes[0] = 400;
    sizes[1] = 400;
    sizes[2] = 400;
    sizes[3] = 400;
    sizes[4] = 800;
    
    var idx;

    for(var i = 0; i < 30; ++i) {
        
        idx = parseInt(Math.random() * 3);
        sizeX = sizes[idx + 1];
        sizeY = sizes[idx];
        
        var newDiv = $("<div style=\"float:left; height:" + sizeY + "px; width: " + sizeX + "px; background-color: " + colors[i % colors.length] + ";\"></div>");
        $("#contentContainer").append(newDiv);
        
        idx = parseInt(Math.random() * 3);
        sizeX = sizes[idx + 1];
        sizeY = sizes[idx];

        var newDiv = $("<div style=\"float:left; height:" + sizeY + "px; width: " + sizeX + "px; background-color: " + colors[(i + 1) % colors.length] + ";\"></div>");
        $("#contentContainer").append(newDiv);

    }

}
*/